package cn.lg.soar.system.biz.modules.general.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.DictDTO;
import cn.lg.soar.system.api.model.DictItemVo;
import cn.lg.soar.system.api.service.IDictApi;
import cn.lg.soar.system.biz.modules.general.entity.DictItem;
import cn.lg.soar.system.biz.modules.general.service.IDictItemService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/dict")
public class DictApiClient implements IDictApi {

    @Autowired
    private IDictItemService service;

    @GetMapping("listByType")
    @Override
    public List<DictItemVo> listByType(@RequestParam("type") String type) {
        return service.listByCode(type);
    }

    @GetMapping("listBatch")
    @Override
    public Map<String, List<DictItemVo>> listBatch(@RequestParam("types") Collection<String> types) {
        return service.mapByCode(types);
    }

    @GetMapping("getDict")
    @Override
    public DictItemVo getDict(@RequestParam("type") String type, @RequestParam("value") Integer value) {
        List<DictItemVo> vos = service.listByCode(type);
        for (DictItemVo vo : vos) {
            if (Objects.equals(vo.getValue(), value)) {
                return vo;
            }
        }
        return null;
    }

    @GetMapping("getLabel")
    @Override
    public String getLabel(@RequestParam("type") String type, @RequestParam("value") Integer value) {
        List<DictItemVo> vos = service.listByCode(type);
        for (DictItemVo vo : vos) {
            if (Objects.equals(vo.getValue(), value)) {
                return vo.getLabel();
            }
        }
        return null;
    }

    private DictDTO toDto(DictItem vo) {
        DictDTO dto = new DictDTO();
        dto.setValue(vo.getValue());
        dto.setLabel(vo.getLabel());
        dto.setMetadata(vo.getMetadata());
        dto.setDisabled(vo.getDisabled());
        return dto;
    }

}
